package com.ki.rent.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ki.rent.entity.Booking;
import com.ki.rent.entity.House;
import com.ki.rent.entity.Style;
import com.ki.rent.entity.User;
import com.ki.rent.mapper.BookingMapper;
import com.ki.rent.service.BookingService;
import com.ki.rent.service.HouseService;
import com.ki.rent.service.StyleService;
import com.ki.rent.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 */
@Controller
@RequestMapping("/rent/booking")
public class BookingController {

    @Autowired
    private BookingService bookingService;
    @Autowired
    private UserService userService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StyleService styleService;

    @RequestMapping("/getHouse")
    public String getHouse(Booking booking, Model model){
        boolean b = bookingService.save(booking);
        if (b){

            return "booking/suc";
        }
        return "";
    }

    @RequestMapping("/back")
    public String back(Model model, HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user = userService.getById(userId);
        model.addAttribute("user",user);
        return "booking/";
    }

    //查看房子预约信息
    @RequestMapping("/hostBooking")
    public String booking(Model model, HttpServletRequest request){
        Integer hostId = (Integer) request.getSession().getAttribute("userId");
        List<Booking> bookings = bookingService.selectHost(hostId);
        model.addAttribute("bookings",bookings);
        return "booking/hostbooking";
    }

    //删除预约信息
    @RequestMapping("/deleteBooking/{bookingId}")
    public String deleteBooking(@PathVariable("bookingId") Integer bookingId,Model model){

        boolean b = bookingService.removeById(bookingId);
        if (b){
            model.addAttribute("msg","删除预约信息成功");
            return "booking/hostbooking";
        }
        model.addAttribute("msg","删除预约信息失败");
        return "booking/hostbooking";
    }


    @RequestMapping("/booking")
    public String nowBooking(Integer styleId, Integer houseId, Model model){

        House house = houseService.getById(houseId);
        house.setStyleId(styleId);
        boolean b = houseService.saveOrUpdate(house);
        if (b){
            model.addAttribute("msg","已为您的房子添加装修样式");
            return "/style/styleindex";
        }
        model.addAttribute("msg","添加装修样式失败");
        return "/style/styleindex";
    }

    @RequestMapping("/toBooking")
    public String toBooking(Integer styleId, Model model, HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<House> houses = houseService.list(wrapper);
        model.addAttribute("houses",houses);
        model.addAttribute("styleId",styleId);
        if (houses == null){
            model.addAttribute("msg","您还没有租赁任何房源哦！");
        }
        return "house/myRentHouse";
    }


}

